﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using nwrole.plugin;
using nwrole.toolbox.sync.ServiceHost;
using nwrole.toolbox.sync.client;

namespace nwrole.toolbox.sync.DestinationPlugin
{
    [Export(typeof(IPlugin))]
    public class SyncDestinationPlugin : IPlugin
    {
        [NwroleConfig("SyncIntervalSeconds")]
        public int SyncIntervalSeconds { get; set; }

        [NwroleConfig("DestDbConnectionString")]
        public string DestDbConnectionString { get; set; }

        // Destination Service Connection Info

        [NwroleConfig("SyncDestServiceConnType")]
        public string SyncDestServiceConnType { get; set; }

        [NwroleConfig("SyncDestServiceSBServiceNamespace")]
        public string SyncDestServiceSBServiceNamespace { get; set; }

        [NwroleConfig("SyncDestServiceSBServicePath")]
        public string SyncDestServiceSBServicePath { get; set; }

        [NwroleConfig("SyncDestServiceSBIssuerName")]
        public string SyncDestServiceSBIssuerName { get; set; }

        [NwroleConfig("SyncDestServiceSBIssuerSecret")]
        public string SyncDestServiceSBIssuerSecret { get; set; }

        [NwroleConfig("SyncDestServiceWSBaseAddress")]
        public string SyncDestServiceWSBaseAddress { get; set; }

        [NwroleConfig("SyncDestServiceWSAddress")]
        public string SyncDestServiceWSAddress { get; set; }

        protected IPluginDebug debug;

        private bool running = false;

        private AutoResetEvent waitEvent = new AutoResetEvent(false);

        public SyncDestinationPlugin()
        {
            SyncIntervalSeconds = 60;
        }

        private void WriteLog(string content)
        {
            Console.WriteLine(content);
            debug.Log(LogLevel.Debug, content, null);
            /*try {
                string logDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "log/TestPluginB/");
                if (!System.IO.Directory.Exists(logDir))
                    System.IO.Directory.CreateDirectory(logDir);

                DateTime now = DateTime.Now;

                System.IO.File.AppendAllText(logDir + "log " + now.ToString("yyyy-MM-dd") + ".txt", now.ToString("yyyy-MM-dd HH:mm:ss ") + content + "\r\n");
            } catch {
            }*/
        }

        public void Start(IPluginDebug debug)
        {
            this.debug = debug;

            WriteLog("SyncDestinationPlugin starting");

            SyncDestinationServiceHost destHost = new SyncDestinationServiceHost();
            destHost.Start(SyncDestinationConnInfo, DestDbConnectionString);

            //SyncDestinationServiceClient client = new SyncDestinationServiceClient(SyncDestinationConnInfo);

            WriteLog("SyncDestinationPlugin running");

            running = true;
            /*while (running)
            {
                WriteLog("SyncDestinationPlugin syncing " + DateTime.Now);
                //client.SyncAllSyncEntities();

                waitEvent.WaitOne(SyncIntervalSeconds * 1000);
            }*/

            waitEvent.WaitOne();

            WriteLog("SyncDestinationPlugin stopped");
        }

        public void Stop()
        {
            WriteLog("SyncDestinationPlugin stopping");
            running = false;
            waitEvent.Set();
        }

        public void ConfigurationChanged()
        {
            WriteLog("SyncDestinationPlugin ConfigurationChanged()");
        }

        public ServiceConnectionInfo SyncDestinationConnInfo
        {
            get
            {
                ServiceConnectionInfo connInfo = new ServiceConnectionInfo();

                ServiceConnectionInfo.ServiceConnectionType type;
                ServiceConnectionInfo.ServiceConnectionType.TryParse(SyncDestServiceConnType, true, out type);
                connInfo.Type = type;

                connInfo.ServiceBusServiceNamespace = SyncDestServiceSBServiceNamespace;
                connInfo.ServiceBusServicePath = SyncDestServiceSBServicePath;
                connInfo.ServiceBusIssuerName = SyncDestServiceSBIssuerName;
                connInfo.ServiceBusIssuerSecret = SyncDestServiceSBIssuerSecret;

                connInfo.WebServiceBaseAddress = SyncDestServiceWSBaseAddress;
                connInfo.WebServiceAddress = SyncDestServiceWSAddress;

                return connInfo;
            }
        }
    }
}
